package com.antwork.common.web.jsp;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;

public class TaglibInterceptor {
	private static Method getRequest = null;

	public void after(JoinPoint joinpoint) {
		try {
			if (getRequest == null) {
				Class context = Class
						.forName("org.apache.struts2.ServletActionContext");
				getRequest = context.getMethod("getRequest", new Class[0]);
			}
			HttpServletRequest request = (HttpServletRequest) getRequest
					.invoke(null, new Object[0]);

			String queryString = request.getQueryString();
			String uri = request.getRequestURI()
					+ ((queryString == null) ? "" : new StringBuilder("?")
							.append(queryString).toString());
			request.setAttribute("uri", uri);
			String searchStr = request.getParameter("q");
			if (searchStr != null) {
				try {
					searchStr = URLDecoder.decode(searchStr, "utf-8");
				} catch (UnsupportedEncodingException localUnsupportedEncodingException) {
				}
				request.setAttribute("q", searchStr);
				String searchType = request.getParameter("qt");
				if (searchType != null) {
					request.setAttribute("qt", searchType);
				}
			}
			String tabStr = request.getParameter("t");
			if (tabStr != null) {
				request.setAttribute("t", tabStr);
			}
			String breadcrumbStr = request.getParameter("b");
			if (breadcrumbStr != null)
				request.setAttribute("b", breadcrumbStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}